package com.ibbt.musicplayer.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ibbt.musicplayer.client.panels.Facebook_UserPanel;
import com.ibbt.musicplayer.client.panels.OverviewStationsPanel;
import com.ibbt.musicplayer.client.panels.RadioStationPanel;
import com.ibbt.musicplayer.model.Radio;
import com.ibbt.musicplayer.model.RadioNoa;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MyMusicPlayer implements EntryPoint {

	private RadioNoa noa = new RadioNoa("urgentfm","http://www.urgent.fm/nowplaying/livetrack.txt",1);
	
	private Radio radio = new Radio("Urgent FM","urgentfm.png","","http://urgent.fm/items/feed",
			"http://www.urgent.fm","http://urgent.stream.flumotion.com/urgent/high.mp3.m3u",
			"http://urgent.stream.flumotion.com/urgent/low.mp3.m3u",noa);
	
	
//	public Radio(String name, String image, String description,
//			String newsRssUrl, String website, String streamHighUrl,
//			String streamLowUrl, RadioNoa radioNoa)
	
	// Panels
	private VerticalPanel verticalPanel_playerbase;
	private DecoratorPanel decoratorPanel_layout;
	private HorizontalPanel horizontalPanel_header;
	private RadioStationPanel radioStationPanel_zender;
	private AbsolutePanel absolutePanel_userframe;
	private DockLayoutPanel controls;
	private DisclosurePanel disclosurePanel_base;
	private TabPanel tabPanel_contents;

	// Buttons
	private ToggleButton button_Extend;
	private ToggleButton button_Play;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Get the RootPanel: the panel to which all other widgets must
		// ultimately be added
		RootPanel rootPanel = RootPanel.get();

		// music player layout with rounded corners via DecoratorPanel
		decoratorPanel_layout = new DecoratorPanel();
		decoratorPanel_layout.addStyleName("musicplayer");
		rootPanel.add(decoratorPanel_layout);

		// vertical panel as base for all the other panels
		verticalPanel_playerbase = new VerticalPanel();
		decoratorPanel_layout.add(verticalPanel_playerbase);

		// header
		try {
			buildHeader();
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// controls
		buildControls();

		// add listeners
		addEvents();

		// base
		buildBase();
	}

	private void addEvents() {
		// Event for EXTEND button
		button_Extend.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				disclosurePanel_base.setOpen(!disclosurePanel_base.isOpen());
			}
		});

		// Event for PLAY button
		button_Play.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

			}
		});

		// Event for Facebook

	}

	private void buildControls() {
		controls = new DockLayoutPanel(Style.Unit.PX);
		controls.addStyleName("controls");

		// Linker controls
		HorizontalPanel leftButtons = new HorizontalPanel();

		Image play = new Image(LayoutResources.INSTANCE.play());
		Image stop = new Image(LayoutResources.INSTANCE.stop());
		button_Play = new ToggleButton(play, stop);
		button_Play.setPixelSize(play.getHeight(), play.getWidth());
		button_Play.setStyleName("button");

		leftButtons.add(button_Play);
		controls.addWest(leftButtons, 250);

		// Rechter controls
		HorizontalPanel rightButtons = new HorizontalPanel();

		Image open = new Image(LayoutResources.INSTANCE.extend_open());
		Image close = new Image(LayoutResources.INSTANCE.extend_close());
		button_Extend = new ToggleButton(open, close);
		button_Extend.setPixelSize(play.getHeight(), play.getWidth());
		button_Extend.setStyleName("button");

		rightButtons.add(button_Extend);
		controls.addEast(rightButtons, 22);

		verticalPanel_playerbase.add(controls);
	}

	private void buildBase() {
		// disclosure panel
		disclosurePanel_base = new DisclosurePanel();
		disclosurePanel_base.setAnimationEnabled(true);
		disclosurePanel_base.setStyleName("disclosurecontent");
		verticalPanel_playerbase.add(disclosurePanel_base);

		// tab panel
		tabPanel_contents = new TabPanel();
		tabPanel_contents.setAnimationEnabled(true);
		tabPanel_contents.getDeckPanel().setStyleName("tabcontents");

		/*******
		 * LATER VERVANGEN DOOR ECHTE TABPANELS - idee: elke tabpanel =
		 * xxxxpanel.java - vb: NowPlayingPanel.java --> NOA / Artist info /
		 * itunesStore / ... StationsPanel.java --> Podcasts & Live Stations
		 * overview RecommendationsPanel.java --> Suggesties ==> RECOMMENDATION
		 * ENGINE MyProfilePanel.java --> FB info / Favorieten / ...
		 */
		HTML a = new HTML("Now Playing");
		HorizontalPanel panelNOA = new HorizontalPanel();
		// panelNOA.setHeight("25px");
		Image now_playing = new Image(LayoutResources.INSTANCE.nowplaying());
		Label labelNOA = new Label("Nu op " + radio.getName());
		labelNOA.addStyleName("tablabel");
		panelNOA.add(now_playing);
		panelNOA.add(labelNOA);
		panelNOA.setWidth("120px");
		panelNOA.setCellVerticalAlignment(labelNOA,
				HorizontalPanel.ALIGN_MIDDLE);
		tabPanel_contents.add(a, panelNOA);

		// HTML b = new HTML("Stations");
		OverviewStationsPanel viewStations = new OverviewStationsPanel();
		HorizontalPanel panelStation = new HorizontalPanel();
		Image stations = new Image(LayoutResources.INSTANCE.stations());
		Label labelStations = new Label("Zenders");
		labelStations.addStyleName("tablabel");
		panelStation.add(stations);
		panelStation.add(labelStations);
		panelStation.setCellVerticalAlignment(labelStations,
				HorizontalPanel.ALIGN_MIDDLE);
		tabPanel_contents.add(viewStations, panelStation);
		// tabPanel_contents.add(b, panelStation);

		HTML c = new HTML("Recommendations");
		HorizontalPanel panelRecommendations = new HorizontalPanel();
		panelRecommendations.setWidth("70px");
		Image foryou = new Image(LayoutResources.INSTANCE.foryou());
		Label labelForYou = new Label("Voor Jou");
		labelStations.addStyleName("tablabel");
		panelRecommendations.add(foryou);
		panelRecommendations.add(labelForYou);
		panelRecommendations.setCellVerticalAlignment(labelForYou,
				HorizontalPanel.ALIGN_MIDDLE);
		tabPanel_contents.add(c, panelRecommendations);

		HTML d = new HTML("My Profile");
		HorizontalPanel panelProfile = new HorizontalPanel();
		panelProfile.setWidth("95px");
		Image myprofile = new Image(LayoutResources.INSTANCE.myprofile());
		Label labelProfile = new Label("Mijn Profiel");
		labelProfile.addStyleName("tablabel");
		panelProfile.add(myprofile);
		panelProfile.add(labelProfile);
		panelProfile.setCellVerticalAlignment(labelProfile,
				HorizontalPanel.ALIGN_MIDDLE);
		tabPanel_contents.add(d, panelProfile);

		tabPanel_contents.selectTab(0);

		/*************************************/

		disclosurePanel_base.add(tabPanel_contents);
	}

	/**
	 * De header bestaat uit een HorizontalPanel opgevuld wordt met twee
	 * AbsolutePanels: -> 1) radioframe = info over radiostation -> 2) userframe
	 * = info over ingelogde gebruiker
	 * @throws RequestException 
	 */
	private void buildHeader() throws RequestException {
		// Header
		horizontalPanel_header = new HorizontalPanel();
		horizontalPanel_header.addStyleName("header");
		verticalPanel_playerbase.add(horizontalPanel_header);

		// Linker frame voor radiostation
		radioStationPanel_zender = new RadioStationPanel(this.radio);
		horizontalPanel_header.add(radioStationPanel_zender);

		// Rechter frame voor gebruikersinfo op basis van facebook account
		absolutePanel_userframe = new Facebook_UserPanel();
		horizontalPanel_header.add(absolutePanel_userframe);
	}



	

	
	

}
